package de.herberlin.contact;

import java.util.logging.Level;

import javax.xml.xpath.XPathExpressionException;

import org.w3c.dom.Document;
// 
/**
 * Class encapsulates xml data of a signle contact;
 * Parses via xpath.
 */
public class Contact {

	/** The document. */
	private Document document = null;

	/**
	 * Instantiates a new contact.
	 * 
	 * @param document the document
	 */
	public Contact(Document document) {
		this.document = document;
	}

	/**
	 * Gets the text entry of the given xpath
	 * 
	 * @param xpath the path
	 * 
	 * @return the text
	 */
	public String getText(String xpath) {
		String result = null;
		try {
			result = Manager.getParser().evaluate(xpath, document);
		} catch (XPathExpressionException e) {
			Manager.getLogger(this).log(Level.SEVERE,"Error with path: " + xpath, e);
		}
		if (result !=null) {
			result=result.trim();
		}
		return result;
	}

	/**
	 * Gets the contaxt entrys name.
	 * 
	 * @return the name
	 */
	public String getName() {
		return getText("//Name/FormattedName");
	}

	/**
	 * Gets the adresses.
	 * 
	 * @return the adresses
	 */
	public String getAdresses() {
		int count = new Integer(
				getText("count(//PhysicalAddressCollection/PhysicalAddress)"));
		StringBuffer buffer=new StringBuffer();
		for (int i=0;i<count;i++) {
			if (i>0) buffer.append(sep);
			
			String s=getText("//PhysicalAddress["+(i+1)+"]/Street");
			if (s !=null && s.length()>0) {
				buffer.append(s);
				buffer.append(" ");
			}
			s=getText("//PhysicalAddress["+(i+1)+"]/PostalCode");
			if (s !=null && s.length()>0) {
				buffer.append(s);
				buffer.append(" ");
			}
			s=getText("//PhysicalAddress["+(i+1)+"]/Locality");
			if (s !=null && s.length()>0) {
				buffer.append(s);
				// buffer.append(" ");
			}
		}
		return buffer.toString();
	}
	
	/**
	 * Gets the email.
	 * 
	 * @return the email
	 */
	public String getEmail() {
		int count = new Integer(
				getText("count(//EmailAddressCollection/EmailAddress)"));
		StringBuffer buffer=new StringBuffer();
		for (int i=0;i<count;i++) {
			String s=getText("//EmailAddressCollection/EmailAddress["+(i+1)+"]/Address");
			if (s !=null && s.length()>0) {
				if (i>0) buffer.append(sep);
				buffer.append(s);
			}
		}
		return buffer.toString();
	}
	
	/**
	 * Gets the phone.
	 * 
	 * @return the phone
	 */
	public String getPhone() {
		int count = new Integer(
				getText("count(//PhoneNumberCollection/PhoneNumber)"));
		StringBuffer buffer=new StringBuffer();
		for (int i=0;i<count;i++) {
//			String s=getText("//PhoneNumberCollection/PhoneNumber["+(i+1)+"]/LabelCollection/Label[1]");
//			if (s !=null && s.length()>0) {
//				buffer.append(s);
//				buffer.append(": ");
//			}
			String s=getText("//PhoneNumberCollection/PhoneNumber["+(i+1)+"]/Number");
			if (s !=null && s.length()>0) {
				buffer.append(s);
				buffer.append(sep);
			}
		}
		return buffer.toString();
	}
	
	/** The sep. */
	private String sep=", ";
	
	/**
	 * Gets  all entries for printing
	 * 
	 * @return the all
	 */
	public String getAll() {
		StringBuffer buffer=new StringBuffer();
		appendToBuffer(buffer,getName());
		appendToBuffer(buffer,getAdresses());
		appendToBuffer(buffer,getEmail());
		appendToBuffer(buffer,getPhone());
		
		String s=buffer.toString();
		if (s.endsWith(sep)) {
			s=s.substring(0,s.length()-sep.length());
		}
		return s;
	}
	
	/**
	 * Appends text to buffer. Handles sep character
	 * 
	 * @param buffer the buffer
	 * @param txt the txt
	 */
	private void appendToBuffer(StringBuffer buffer, String txt) {
		if (txt != null && txt.length()>0) {
			buffer.append(txt);
			if (!txt.endsWith(sep)) {
				buffer.append(sep);
			}
		}
	}
}
